<?php
namespace app\admin\controller;

use think\Controller;
use app\admin\logic\UserModel;
use app\admin\logic\UserLogic;
/**
 * 登录管理
 */
class Login extends Controller
{

    public function index()
    {

        $user = new UserLogic();
        $res  = $user->check_login_status();
        if ($res) {
            $this->redirect('admin/index/index');
            exit;
        }
        
        return $this->fetch("index");
    }

    // 登录信息
    public function enter_web()
    {
        $user = new UserLogic();
        $name = input("param.name/s");
        $pass = input("param.password/s");

        if (request()->isAjax()) {
            // 验证账号密码
            $res = $user->check_account($name, $pass);
            if (isset($res['msg'])) {
                echo json_encode($res);
                exit;
            }

            // 记录登录日志
            $re = $user->save_login_log($res['user_id']);
            if (isset($re['msg'])) {
                echo json_encode("登录失败");
                exit;
            }

            // 登录...
            $info = $res;
            unset($info['password']);
            unset($info['salt']);
            unset($info['status']);
            // 记录时间
            $info['login_time']  = date("Y-m-d H:i:s",time());
            $info['active_time'] = date("Y-m-d H:i:s",time());

            session('user', null);
            session('user', $info);

            echo json_encode(msg_success("登录成功"));
            exit;

        }
    }

    // 退出登录
    public function login_out()
    {

        $info = session('user');
        if (!empty($info)) {
            session('user', null);
        }
        
        // 跳转到登录
        $this->redirect('index');
    }    

}
